<?php
# Incluimos la configuracion
include ("includes/header.php");
include ("includes/connection.php");
include ("includes/login.php");
include ("includes/iniciaSessio.php");
# Obtenemos el mensaje privado
$id = $_GET['id'];
$sql = "SELECT * FROM missatge WHERE per='" . $_SESSION['username'] . "' and idMissatge='" . $id . "'";
$res = mysql_query($sql, $conexion) or die(mysql_error());
$row = mysql_fetch_assoc($res);
?>
<?php
include ("includes/contentHeader.php");
include ("./includes/menuMissatges.php");
?>
<!--contingut 1-->
<section class="container">
	<!--fons gris-->
	<div class="hero-unit">
		<br>
		<strong>De: </strong><?php echo $row['de']
		?><br />
		<strong>Data: </strong><?php echo $row['data']
		?><br />
		<strong>Assumpte: </strong><?php echo $row['assumpte']
		?><br />
		<br />
		<strong>Missatge: </strong>
		<br />
		<?php echo $row['text']
		?>

		<br />
		<br />
		<button class="btn btn-primary" data-toggle="modal" data-target="#myModal">
			Contestar
		</button>
		<?php
		# Avisamos que ya lo leimos
		if ($row['llegit'] != 1) {
			mysql_query("UPDATE missatge SET llegit=1 where idMissatge='" . $id . "'", $conexion) or die(mysql_error());
		}
		?>
	</div>
	<!-- fi fons gris-->
</section>
<!-- fi contingut 1-->
<!--contingut 2-->
<section class="container">
	<div class="hero-unit">
		<!--finestre modal-->
		<div  id="myModal" class="modal hide fade">
			<div class="modal-header">
				<a class="close" data-dismiss="modal">×</a>
			</div>
			<div class="modal-body">
				<form class="form-inline" action="#" method="post">
					<div class="control-group">
						Para:
						<br />
						<input readonly="readonly" type="text" name="per" value="<?=$row['de']
						?>"/>
						<br />
						Asunto:
						<br />
						<input type="text" name="assumpte" />
						<br />
						Mensaje:
						<br />
						<textarea name="text"></textarea>
						</br></br> <a class="enviarFrm btn btn-danger" data-dismiss="modal">Cancel·lar</a>
						<button class="enviarFrm btn btn-success" name="enviar" type="submit" value="Enviar missatge" >
							Enviar
						</button>
				</form>
				<?php
				if (isset($_POST['enviar'])) {
					if (isset($_POST['assumpte']) && isset($_POST['text'])) {
						$data = date("j/m/Y, g:i a");
						$per = $_POST['per'];
						$de = $_SESSION['username'];
						$assumpte = $_POST['assumpte'];
						$text = $_POST['text'];
						$estat = 0;
						$sql = "INSERT INTO missatge (per ,de ,data ,assumpte ,text ,llegit) VALUES ('$per', '$de', '$data', '$assumpte', '$text', '$estat')";
						if (mysql_query($sql, $conexion)) {
							echo "	<script>
alert('Mensaje enviado correctamente');
</script>";
						} else {
							echo "	<script>
alert('Error al enviar mensaje');</script>";
						}
					}
				}
				?>
			</div>
		</div>
		<!--<div class="modal-footer">
		<a href="#" class="btn">Close</a>
		<a href="#" class="btn btn-primary">Save changes</a>
		</div>-->
	</div><!--fi finestra-->
	</div>
</section>
<!-- fi contingut 2-->
<hr>
<?php
include ("includes/footer.php");
?>